package com.zhoug.zmui.list;

import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;

/**
 * @Author: zhoug
 * @Date: 2024-07-05
 * @Description:
 */
public class ListSelectHelper<T> {
    private OnSelectChangeListener<T> mListener;

    /**
     * 选择的数据
     */
    private List<T> mSelectData;
    /**
     * 是否显示选择图标
     */
    private boolean mShowSelectIcon;
    /**
     * 多选
     */
    private boolean mMultiSelect = false;

    /**
     * 是否支持取消选中
     */
    protected boolean mCancelSelectEnabled = true;
    /**
     * 是否启用
     */
    protected boolean mEnabled = true;

    private int textAlign;//列表文字对齐方式

    private SelectListAdapter mListAdapter;

    public ListSelectHelper(SelectListAdapter<T> adapter) {
        mListAdapter = adapter;
        mSelectData = new ArrayList<>();
    }

    public void showSelectImage(boolean show) {
        mShowSelectIcon = show;
    }

    public boolean isShowSelectImage() {
        return mShowSelectIcon;
    }

    public void setSelect(List<T> data) {
        mSelectData.clear();
        if (data != null && !data.isEmpty()) {
            if (!mMultiSelect && data.size() > 1) {
                throw new RuntimeException("当前为单选 data size 大于 1");
            }
            mSelectData.addAll(data);
        }
    }

    public void setSelect(T data) {
        mSelectData.clear();
        if (data != null) {
            mSelectData.add(data);
        }
    }

    public void addSelect(List<T> data) {
        if (data != null && !data.isEmpty()) {
            if (!mMultiSelect && data.size() > 1) {
                throw new RuntimeException("当前为单选 data size 大于 1");
            }
            if (mMultiSelect) {
                mSelectData.addAll(data);
            } else {
                mSelectData.clear();
                mSelectData.addAll(data);
            }
        }
    }

    public void addSelect(T data) {
        if (data != null) {
            if (!mMultiSelect) {
                mSelectData.clear();
            }
            mSelectData.add(data);
        }
    }

    public void clearSelect(T data) {
        if (data != null && !mSelectData.isEmpty()) {
            if (mMultiSelect) {
                mSelectData.remove(data);
            } else {
                mSelectData.clear();
            }
        }
    }


    public void clearSelect() {
        mSelectData.clear();
    }


    @Nullable
    public List<T> getSelect() {
        if (!mSelectData.isEmpty()) {
            return new ArrayList<>(mSelectData);
        }
        return null;
    }

    public T getSingleSelect() {
        if (!mSelectData.isEmpty()) {
            return mSelectData.get(0);
        }
        return null;
    }

    public void setMultiSelect(boolean multi) {
        if (mMultiSelect != multi) {
            mMultiSelect = multi;
            mSelectData.clear();
        }
    }

    public void setCancelSelectEnabled(boolean cancelSelectEnabled) {
        this.mCancelSelectEnabled = cancelSelectEnabled;
    }

    public void setEnabled(boolean enabled) {
        this.mEnabled = enabled;
    }

    public boolean isCancelSelectEnabled() {
        return this.mCancelSelectEnabled;
    }

    public boolean isSelect(T data) {
        return !mSelectData.isEmpty() && mSelectData.contains(data);
    }


    public boolean isMultiSelect() {
        return mMultiSelect;
    }

    public void setOnSelectChangeListener(OnSelectChangeListener<T> onSelectChangeListener) {
        this.mListener = onSelectChangeListener;
    }

    /**
     * 触发回调
     */
    public void onSelectChange() {
        if (mListener != null) {
            if (mSelectData.isEmpty()) {
                mListener.onClearSelect();
            } else {
                mListener.onSelectChange(getSelect());
            }
        }
    }

    /**
     * 设置对齐方式
     *
     * @param textAlign {@link android.view.Gravity#CENTER}
     */
    public void setTextAlign(int textAlign) {
        this.textAlign = textAlign;
    }

    public int getTextAlign() {
        return textAlign;
    }

    public void bindData(T data, View itemView, TextView textView, View iconView) {
        boolean select = isSelect(data);
        itemView.setSelected(select);
        if (iconView != null) {
            if (isShowSelectImage()) {
                iconView.setVisibility(select ? View.VISIBLE : View.INVISIBLE);
            } else {
                iconView.setVisibility(View.GONE);
            }
        }
        if (textView != null) {
            textView.setSelected(select);
            if (textAlign != 0) {
                textView.setGravity(textAlign);
            }
        }

        itemView.setTag(data);
        itemView.setClickable(true);
        itemView.setOnClickListener(onClickListener);
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!mEnabled) {
                return;
            }
            T itemData = (T) v.getTag();
            if (isSelect(itemData)) {
                //选中->不选中
                if (!isCancelSelectEnabled()) {
                    return;
                }
                clearSelect(itemData);
            } else {
                //不选中->选中
                addSelect(itemData);
            }
            mListAdapter.notifyDataChange();
            onSelectChange();
        }
    };


}
